#include <stdio.h>
#include <stdlib.h>	/* exit() */
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>


/*
 * 修改 book-06 以避免竞争条件.
 */


static void charatatime(char * str)
{
	char * ptr;
	int c;

	setbuf(stdout, NULL);

	for (ptr = str; (c = *ptr++) != 0; )
		putc(c, stdout);
}

int main(void)
{
	pid_t pid;

	TELL_WAIT();

	if (( pid = fork()) < 0) 
		perror("fork error");
	else if (pid == 0) {	/* first child */
		WAIT_PARENT();		/* parent goes first */
		charatatime("output from child\n");
	}
	else {
		charatatime("output from parent\n");
		TELL_CHILD(pid);
	}

	exit(0);
}

